#pragma once
#include<iostream>
#include<string>
#include<json/json.h>
typedef struct request
{
    int x;
    int y;
    char op;
    request(){}
    request(int datax,int datay,char opera)
    :x(datax),y(datay),op(opera)
    {
    }
}request_t;
typedef struct response
{
    private:
    int _x;
    int _y;
    char _op;
    public:
    int code;
    int result;//0为正常,1除0错误，-1,2程序错误
    response()
    {
        code = -1;
        result = 0;
    }
    response(int x,int y,char op):_x(x),_y(y),_op(op){}    
    response run()
    {
        return run(_x,_y,_op);
    }
    response run(int x ,int y,char op)
    {
        switch(op)
        {
            case '+':
            this->result = x + y;
            this->code = 0;
            break;
            case '-':
            this->result = x - y;
            this->code = 0;
            break;
            case '*':
            this->result = x * y;
            this->code = 0;
            break;
            case '/':
            if(y == 0)
            {
                this->code = 1;
                break;
            }
            this->result = x/y;
            this->code = 0;
            break;
            default:
            this->code = 2;
            break;
        }
        return *this;
    }
}response_t;
//序列化
std::string SerializeRequest(const request_t &req)
{
    Json::Value root;
    root["datax"] = req.x;
    root["datay"] = req.y;
    root["operator"] = req.op;
    Json::FastWriter writer;
    std::string json_string = writer.write(root);
    return json_string;
}
//反序列化
void DeserializeRequest(const std::string &json_string,request_t &out)
{
    Json::Reader reader;
    Json::Value root;
    reader.parse(json_string,root);
    out.x = root["datax"].asInt();
    out.y = root["datay"].asInt();
    out.op = (char)root["operator"].asInt();
}
//序列化
std::string SerializeResponse(const response_t &res)
{
    Json::Value root;
    Json::FastWriter writer;
    root["code"] = res.code;
    root["result"] = res.result;
    std::string json_string = writer.write(root);
    return json_string;
}
//反序列化
void DeSerializeResponse(const std::string &json_string,response_t &out)
{
    Json::Value root;
    Json::Reader reader;
    reader.parse(json_string,root);
    out.code = root["code"].asInt();
    out.result = root["result"].asInt();
}
